package com.wutuo.aggragation.roster.controller;

import cn.apestar.agg.user.info.service.UserService;
import cn.apestar.core.base.BaseMessage;
import cn.apestar.dimension.enums.BaseConstant;
import cn.apestar.friend.api.WtEliteAPIService;
import cn.apestar.user.provider.info.cache.UserCacheProvider;
import com.wutuo.roster.api.RosterAPIService;
import com.wutuo.roster.provider.constant.GradeUtil;
import com.wutuo.roster.provider.constant.ZxingHandler;
import com.wutuo.roster.provider.enums.EnumList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公共类 <br>
 * 详细说明.
 * <p>
 * Copyright: Copyright (c) 2016年1月26日 下午10:21:39
 * <p>
 *
 * @author lvh
 * @version 1.0.0
 */
@RestController @RequestMapping(value = "/commonWT", produces = "application/json")
public class CommonWTController {

    private static String url = "";

    private Logger logger = LoggerFactory.getLogger(CommonWTController.class);

    @Autowired private RosterAPIService rosterService;

    @Autowired private UserCacheProvider userCacheProvider;

    @Autowired private WtEliteAPIService eliteService;

    @Autowired private UserService userService;

    /**
     * 查询年度
     */
    @RequestMapping(value = "/session", method = RequestMethod.GET) public Object session() {

        return GradeUtil.getGradeList();
    }

    /**
     * 查询班级
     */
    @RequestMapping(value = "/grade", method = RequestMethod.GET) public Object grade() {

        Map mapGrade = new HashMap();
        mapGrade.put(1, "一");
        mapGrade.put(2, "二");
        mapGrade.put(3, "三");
        mapGrade.put(4, "四");
        mapGrade.put(5, "五");
        mapGrade.put(6, "六");
        mapGrade.put(7, "七");
        mapGrade.put(8, "八");
        mapGrade.put(9, "九");
        mapGrade.put(10, "十");
        List list = new ArrayList();
        for (int i = 1; i < 20; i++) {
            Map map = new HashMap();
            if (mapGrade.containsKey(i)) {
                map.put("grade", mapGrade.get(i) + "班");
            } else {
                map.put("grade", mapGrade.get(10).toString() + mapGrade.get(i - 10) + "班");
            }
            list.add(map);
        }
        Map mapOther = new HashMap();
        mapOther.put("grade", "其他");
        return list;
    }

    /**
     * 取得用户类型
     */
    @RequestMapping(value = "/userType", method = RequestMethod.GET)
    public List<HashMap> getUserType() {

        return EnumList.getUserEnumList();
    }

    @RequestMapping(value = "/getCommonList", method = RequestMethod.GET)
    public Map getCommonList() {

        Map map = new HashMap();
        map.put("userType", getUserType());
        map.put("grade", grade());
        map.put("session", session());
        // 朋友圈年级
        map.put("elite_grade", EnumList.getGradeEnumList());
        // 朋友圈类型
        map.put("elite_type", EnumList.getEliteTypeSEnumList());
        // 获指标类型
        map.put("index_chioce", EnumList.getEliteChoiceEnumList());
        // 获取孩子类型
        map.put("child_type", EnumList.getEliteChildTypeSEnumList());
        // 本周
        map.put("week", EnumList.getWeekList());
        // 本月
        map.put("month", EnumList.getMonthList());
        return map;
    }

    /**
     * 取得二维码URL
     */
    @RequestMapping(value = "/getTwoDuRL", method = RequestMethod.GET) public String getTwoDuRL(
        @RequestParam("url") String url) {

        String content = URLDecoder.decode(url);
        String urls = ZxingHandler.encodeQny(content, 10, 10, BaseConstant.TWODIMENSIONAL);
        return urls;
    }

    /**
     * 清空缓存
     */
    @RequestMapping(value = "/clearCache/id/{userId}/type/{type}", method = RequestMethod.GET)
    public Object clearCache(@PathVariable("userId") Long userId,
        @PathVariable("type") String type) {

        userCacheProvider.clearUserInfoCache(userId);
        return new BaseMessage();
    }

    /**
     * 删除所有的KEY
     */
    @RequestMapping(value = "/clearAllCache", method = RequestMethod.GET)
    public Object clearAllCache() {

        eliteService.clearAllCache();
        return new BaseMessage();
    }

    /**
     * 此借口专门服务于管理后台清除缓存使用，待模块化后删除此方法
     *
     * @param userId
     * @return void
     * @author Shunqin.Chen 2016年4月5日 下午3:28:09</br>
     */
    @RequestMapping(value = "/clear/cache/{user_id}", method = RequestMethod.GET)
    public Object clearUserCache(@PathVariable("user_id") Long userId) {

        if (userId == null) {
            return new BaseMessage();
        }
        userService.clearUserCache(userId);
        return new BaseMessage();
    }

    /**
     * 此借口专门服务于增加灰度用户
     *
     * @param userId
     * @return void
     * @author Shunqin.Chen 2016年4月5日 下午3:28:09</br>
     */
    @RequestMapping(value = "/addGray/{user_id}", method = RequestMethod.GET)
    public Object addGrayUser(@PathVariable("user_id") Long userId) {

        if (userId == null) {
            return new BaseMessage();
        }
        userService.clearUserCache(userId);
        return new BaseMessage();
    }

}
